package com.example.dorm1_demo.controller;

import com.example.dorm1_demo.entity.Dormitory;
import com.example.dorm1_demo.entity.Student;
import com.example.dorm1_demo.service.DormitoryService;
import com.example.dorm1_demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private DormitoryService dormitoryService;

    // 获取所有学生列表
    @GetMapping("/list")
    public String listStudents(Model model) {
        model.addAttribute("students", studentService.getAllStudents());
        return "student-list";
    }
    // 根据姓名搜索学生
    @GetMapping("/search")
    public String searchStudents(@RequestParam String name, Model model) {
        model.addAttribute("students", studentService.searchStudentsByName(name));
        return "student-list";
    }

    // 显示添加学生页面
    @GetMapping("/add")
    public String addStudentPage(Model model) {
        model.addAttribute("dormitories", dormitoryService.getAllDormitories());
        return "student-add";
    }

    //添加新学生
    @PostMapping("/add")
    public String addStudent(Student student) {
        // 如果分配了宿舍，更新宿舍入住情况和宿舍号
        if (student.getDormitoryId() != null && student.getDormitoryId() > 0) {
            Dormitory dormitory = dormitoryService.getDormitoryById(student.getDormitoryId());
            if (dormitory != null) {
                student.setDormitoryNumber(dormitory.getDormitoryNumber());
                dormitoryService.updateDormitoryOccupancy(student.getDormitoryId(), true);
            }
        }
        studentService.addStudent(student);
        return "redirect:/student/list";
    }

    // 编辑学生信息
    @PostMapping("/edit")
    public String editStudent(Student student) {
        Student oldStudent = studentService.getStudentById(student.getId());
        // 处理宿舍变更
        if (oldStudent.getDormitoryId() != null && !oldStudent.getDormitoryId().equals(student.getDormitoryId())) {
            // 原宿舍减少一人
            if (oldStudent.getDormitoryId() > 0) {
                dormitoryService.updateDormitoryOccupancy(oldStudent.getDormitoryId(), false);
            }
            // 新宿舍增加一人
            if (student.getDormitoryId() != null && student.getDormitoryId() > 0) {
                Dormitory dormitory = dormitoryService.getDormitoryById(student.getDormitoryId());
                if (dormitory != null) {
                    student.setDormitoryNumber(dormitory.getDormitoryNumber());
                    dormitoryService.updateDormitoryOccupancy(student.getDormitoryId(), true);
                }
            } else {
                student.setDormitoryNumber(null);
            }
        }
        studentService.updateStudent(student);
        return "redirect:/student/list";
    }

    // 删除学生信息
    @GetMapping("/delete/{id}")
    public String deleteStudent(@PathVariable Integer id) {
        Student student = studentService.getStudentById(id);
        if (student.getDormitoryId() != null && student.getDormitoryId() > 0) {
            dormitoryService.updateDormitoryOccupancy(student.getDormitoryId(), false);
        }
        studentService.deleteStudent(id);
        return "redirect:/student/list";
    }

    // 显示编辑学生页面
    @GetMapping("/edit/{id}")
    public String editStudentPage(@PathVariable Integer id, Model model) {
        model.addAttribute("student", studentService.getStudentById(id));
        model.addAttribute("dormitories", dormitoryService.getAllDormitories());
        return "student-edit";
    }

    // 取指定宿舍的所有学生
    @GetMapping("/dormitory/{dormitoryId}")
    public String getStudentsByDormitory(@PathVariable Integer dormitoryId, Model model) {
        model.addAttribute("dormitory", dormitoryService.getDormitoryById(dormitoryId));
        model.addAttribute("students", studentService.getStudentsByDormitoryId(dormitoryId));
        return "dormitory-students";
    }
}